<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <link rel="stylesheet" href="../static/css/bootstrap.css">
    <script src="../static/js/jquery.js"></script>
    <script src="../static/js/bootstrap.js"></script>
    <style>
        .container {
            max-width: 500px;
        }
        h3 {
            color: #0084ff;
            font-weight: bold;
        }
        form>div {
            margin-top: 20px;
        }
        label {
            font-weight: lighter;
        }
        .btn {
            margin-left: 35%;
        }
    </style>
</head>
<body>
    <div class="container">
        <h3>
            密码设置
        </h3>
        <form action="#">
            <div>
                <label for="">旧密码</label>
                <div>
                    <input id="oldPassword" type="password" class="form-control">
                </div>
            </div>
            <div>
                <label>
                    新密码
                </label>
                <div>
                    <input id="newPassword" type="password" class="form-control">
                </div>
            </div>
            <div>
                <label>
                    确认密码
                </label>
                <div>
                    <input id="againNewPassword" type="password" class="form-control">
                </div>
            </div>
            <div>
                <button class="submit-update btn btn-primary">确认修改</button>
            </div>
        </form>
    </div>

</body>
</html>
<script>

    var curWwwPath = window.location.href;
    var pathName = window.location.pathname;
    var ctx = curWwwPath.substring(0,curWwwPath.indexOf(pathName));

    $(document).ready(function () {
        $(".submit-update").click(function () {
            if (!checkPassword()) {
                alert("两次新密码输入不同，请重新输入");
            } else {
                updatePassword();
                return false;
            }
        })
    })

    function checkPassword() {
        var newPwd = $("#newPassword").val();
        var againNewPwd = $("#againNewPassword").val();
        if (newPwd != againNewPwd) {
            return false;
        } else {
            return true;
        }
    }

    function updatePassword() {
        var url = ctx + "/person/updatePassword";
        var oldPassword = $("#oldPassword").val();
        var newPassword = $("#newPassword").val();
        var data = "oldPassword=" + oldPassword + "&newPassword=" + newPassword;
        $.ajax({
            "url": url,
            "data": data,
            "dataType": "json",
            "type": "Post",
            "success": function (json) {
                if (json.state == 200) {
                    alert("修改成功，请重新登录...")
                    top.location.href = "/user/toLogin";
                } else if (json.state == 405) {
                    alert(json.message)
                }
            }
        })
    }
</script>